#include "printlog.h"
#include <QCoreApplication>
#include <QDir>
#include <QDateTime>
#include <QTime>
#include <QDebug>


PrintLog::PrintLog(QObject *parent) : QObject(parent)
  , m_logExePaths("")
{
    m_logExePaths = QCoreApplication::applicationDirPath();

    m_logExePaths += "/logs/";

    QDir wdir(m_logExePaths);
    QDir isDir;

    //设置顶端配置路径
    if (!isDir.exists(m_logExePaths+"cotests/" )) {


        wdir.mkpath("cotests" );
    }
    m_logExePaths += "/cotests/";

}

PrintLog::~PrintLog()
{

}

PrintLog *PrintLog::gestance()
{
    static PrintLog  printLog;

    return  &printLog;
}

void PrintLog::printInfor(QString infors)
{
    QDateTime currentDateTimes = QDateTime::currentDateTime();

    qint64 currentTimes = currentDateTimes.currentSecsSinceEpoch();
     currentTimes =  currentTimes / 360 ;
    QString currentDateStr = currentDateTimes.toString("yyyy-MM-dd");
    QString currentDateStra = currentDateTimes.toString("yyyy-MM-dd hh:mm:ss =>");


    qDebug()<<currentDateStra<<infors;
    QString currentStr = QString("_%1").arg(currentTimes);

    QDir wdir(m_logExePaths);
    QDir isDir;

    //设置顶端配置路径
    if (!isDir.exists(m_logExePaths+currentDateStr )) {


        wdir.mkpath(currentDateStr );
    }
    QString filePaths = m_logExePaths+currentDateStr;

    filePaths +="/"+currentDateStr+currentStr+".log";

    QFile file(filePaths);
    if(file.open(QIODevice::Append)){
        file.write(currentDateStra.toUtf8());

        file.write(infors.toUtf8());
        file.write("\n");

        file.flush();
    }

    file.close();
}
